<?php

// 查立得PHP+mysql通用预约排队系统 V2024.12.12
// 演示地址: http://duilie.chalide.cn
// 文件路径: login.php
// 文件大小: 6790 字节
// 最后修改时间: 2024-12-17 21:00:00
// 作者: yujianyue
// 邮件: 15058593138@qq.com
// 版权所有,保留发行权和署名权
define('IN_SYSTEM', true);
require_once 'inc/conn.php';
require_once 'inc/pubs.php';
require_once 'inc/json.php';

session_start();

// 检查是否已登录
if(isset($_SESSION['admin'])) {
    header('Location: admin.php');
    exit;
} elseif(isset($_SESSION['doctor'])) {
    header('Location: hexiao.php');
    exit;
}

// 处理AJAX登录请求
if(isset($_GET['act']) && $_GET['act'] == 'login') {
    $username = isset($_POST['username']) ? safe_string($_POST['username']) : '';
    $password = isset($_POST['password']) ? safe_string($_POST['password']) : '';
    $type = isset($_POST['type']) ? safe_string($_POST['type']) : '';
    
    if(empty($username) || empty($password) || empty($type)) {
        json_msg(0, '请填写完整信息');
    }
    
    $password = md5($password);
    
    if($type == 'admin') {
        // 管理员登录
        $sql = "SELECT id FROM admin WHERE username = '$username' AND password = '$password'";
        $admin = $conn->query($sql)->fetch_assoc();
        
        if($admin) {
            $_SESSION['admin'] = $admin;
            $conn->query("UPDATE admin SET last_login = NOW() WHERE id = {$admin['id']}");
            json_msg(1, '登录成功', array('url' => 'admin.php'));
        }
    } else {
        // 医生登录
        $sql = "SELECT id, name, department FROM doctor 
                WHERE username = '$username' AND password = '$password'";
        $doctor = $conn->query($sql)->fetch_assoc();
        
        if($doctor) {
            $_SESSION['doctor'] = $doctor;
            $conn->query("UPDATE doctor SET last_login = NOW() WHERE id = {$doctor['id']}");
            json_msg(1, '登录成功', array('url' => 'hexiao.php'));
        }
    }
    
    json_msg(0, '用户名或密码错误');
    exit;
}
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录 - <?php echo $CONFIG['site_title']; ?></title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: Arial, sans-serif;
            background: #f5f5f5;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
        }
        
        .login-box {
            background: white;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            width: 100%;
            max-width: 400px;
        }
        
        .login-title {
            text-align: center;
            margin-bottom: 30px;
            color: #333;
            font-size: 24px;
        }
        
        .form-item {
            margin-bottom: 20px;
        }
        
        .form-item input, .form-item select {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 16px;
            transition: border-color 0.3s;
        }
        
        .form-item input:focus, .form-item select:focus {
            border-color: #007bff;
            outline: none;
        }
        
        .btn-login {
            width: 100%;
            padding: 12px;
            background: #007bff;
            color: white;
            border: none;
            border-radius: 4px;
            font-size: 16px;
            cursor: pointer;
            transition: background 0.3s;
        }
        
        .btn-login:hover {
            background: #0056b3;
        }
        
        .error-msg {
            color: #dc3545;
            text-align: center;
            margin-top: 10px;
            display: none;
        }
        
        /* 动画效果 */
        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(-20px); }
            to { opacity: 1; transform: translateY(0); }
        }
        
        .login-box {
            animation: fadeIn 0.5s ease;
        }
    </style>
</head>
<body>
    <div class="login-box">
        <h1 class="login-title"><?php echo $CONFIG['site_title']; ?></h1>
        
        <div class="form-item">
            <input type="text" id="username" placeholder="请输入用户名" autocomplete="off">
        </div>
        
        <div class="form-item">
            <input type="password" id="password" placeholder="请输入密码">
        </div>
        
        <div class="form-item">
            <select id="type">
                <option value="doctor">医生登录</option>
                <option value="admin">管理员登录</option>
            </select>
        </div>
        
        <button class="btn-login" onclick="doLogin()">登 录</button>
        
        <div id="error" class="error-msg"></div>
    </div>

    <script src="inc/js.js"></script>
    <script>
        // 按Enter键登录
        document.onkeydown = function(e) {
            if(e.keyCode == 13) {
                doLogin();
            }
        };
        
        // 登录处理
        function doLogin() {
            var username = document.getElementById('username').value;
            var password = document.getElementById('password').value;
            var type = document.getElementById('type').value;
            var error = document.getElementById('error');
            
            if(!username || !password) {
                error.style.display = 'block';
                error.textContent = '请输入用户名和密码';
                return;
            }
            
            // 隐藏错误提示
            error.style.display = 'none';
            
            // 发送登录请求
            ajax({
                url: '?act=login',
                type: 'POST',
                data: {
                    act: 'login',
                    username: username,
                    password: password,
                    type: type
                },
                success: function(res) {
                    var data = JSON.parse(res);
                    if(data.code === 1) {
                        location.href = data.data.url;
                    } else {
                        error.style.display = 'block';
                        error.textContent = data.msg;
                    }
                },
                error: function() {
                    error.style.display = 'block';
                    error.textContent = '网络错误，请重试';
                }
            });
        }
    </script>
</body>
</html> 